package com.lsa.southwind.controller;


import com.lsa.southwind.entity.Building;
import com.lsa.southwind.form.SearchForm;
import com.lsa.southwind.service.BuildingService;
import com.lsa.southwind.util.ResultVoUtil;
import com.lsa.southwind.vo.PageVo;
import com.lsa.southwind.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/building")
public class BuildingController {
    @Autowired
    private BuildingService buildingService;

    @PostMapping("/save")
    public ResultVo save(@RequestBody Building building) {
        boolean save = this.buildingService.save(building);
        if (!save) {
            return ResultVoUtil.fail();
        } else {
            return ResultVoUtil.success(null);
        }

    }

    @GetMapping("/list/{page}/{size}")
    public ResultVo buildinglist(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        PageVo buildinglist = this.buildingService.list(page, size);
        return ResultVoUtil.success(buildinglist);
    }

    @GetMapping("/search")
    public ResultVo search(SearchForm searchForm) {
        PageVo searchList = this.buildingService.search(searchForm);
        return ResultVoUtil.success(searchList);
    }

    /*学生修改*/
    /*修改信息回填*/
    @GetMapping("/findById/{id}")
    public ResultVo findById(@PathVariable("id") Integer id) {
        Building buildingone = this.buildingService.getById(id);
        return ResultVoUtil.success(buildingone);
    }

    @PutMapping("/update")
    public ResultVo update(@RequestBody Building building) {
        boolean update = this.buildingService.updateById(building);
        if (!update) {
            return ResultVoUtil.fail();
        } else {
            return ResultVoUtil.success(null);
        }
    }

    @DeleteMapping("/deleteById/{id}")
    public ResultVo deleteById(@PathVariable("id") Integer id) {
        Boolean deleteById = this.buildingService.deleteById(id);
        if (!deleteById) {
            return ResultVoUtil.fail();
        } else {
            return ResultVoUtil.success(null);
        }
    }

    @GetMapping("/list")
    public ResultVo list() {
        List<Building> buildingList = this.buildingService.list();
        return ResultVoUtil.success(buildingList);
    }
}

